/*
 * JniHFileConstant.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.jni;

import java.io.IOException;
import java.io.PrintWriter;

import com.thoughtworks.qdox.model.JavaField;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileText;
import com.cute4j.tools.ng.Utils;

/**
 * JNI File Constant.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class JniHFileConstant extends NativeFileText {

// Private Fields. -------------------------------------------------------------

  private final JavaField field;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new JniHFileConstant object.
   *
   * @param nativeFile AbstractNativeFile
   * @param field the qdox java field
   */
  public JniHFileConstant(AbstractNativeFile nativeFile, JavaField field) {
    super(nativeFile);
    this.field = field;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void storeContent(PrintWriter pw) throws IOException {
    if (field.getType().isPrimitive()) {
      String name = Utils.getJniConstantName(field);
      pw.print("#undef ");
      pw.println(name);
      pw.print("#define ");
      pw.print(name);
      pw.print(" ");
      pw.println(filterInitializationExpressionBug(
                                  field.getInitializationExpression().trim()));
    }
  }

// Private Fields. -------------------------------------------------------------

  private String filterInitializationExpressionBug(String text) {
    return text.substring(text.lastIndexOf(' ') + 1);
  }

}
